<?php
namespace  Admin\Controller;
use        Think\Controller;

class DomainController  extends  AdminController{
public function index(){
			      // 搜索

        $keyword = I('keyword', '', 'string');

        $condition = array('like','%' . $keyword . '%');

        $map['name|id'] = array($condition,$condition,'_multi' => true );

 

        // 获取所有用户

        

        $data_list = D('Domain')->page(! empty($_GET["p"]) ? $_GET["p"] : 1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();   

        $page = new \Common\Util\Page(D('Domain')->where($map)->count(), C('ADMIN_PAGE_ROWS'));

        

        foreach ($data_list as $key => &$val) {
         $customerid=$val['customer'];
 
          $val['c_user']=get_user_info($val['c_user'],"username");
          $val['customer']=get_customer_info($val['customer'],"customer_name");
             
          $val['customer']="<a href='".U('customer/xq',array('id'=>$customerid))."' style='text-decoration:none'>".$val['customer']."</a>";            
					 
            $tt1['tablename']='server_buyer';
						$tt1['fieldvalue']=$val['register'];
						$server_buyer=D('Descriptions')->where($tt1)->find();
						$val['register']=$server_buyer['title'];
        
       
       
        }

     

        // 使用Builder快速建立列表页面。

        $builder = new \Common\Builder\ListBuilder();

        $builder->setMetaTitle('域名列表')

            ->addTopButton('addnew') // 设置页面标题

            ->addTopButton('delete') // 添加新增按钮

            ->setSearch('请输入域名名称/id', U('index')) // 添加删除按钮



            ->addTableColumn('name', '域名')

            ->addTableColumn('register', '注册商')

            ->addTableColumn('psd', '管理密码')

             ->addTableColumn('customer', '客户')		

         ->addTableColumn('price', '续费价格')
            ->addTableColumn('r_time', '注册时间','time')

            ->addTableColumn('e_time', '过期时间','time')

			->addTableColumn('c_user', '创建人')

        	  ->addTableColumn('remark', '备注')

         

 	     	 

            ->addTableColumn('right_button', '操作', 'btn')

            ->setTableDataList($data_list)

            ->setTableDataPage($page->show())

            ->addRightButton('edit')

            ->display();
	}
	
	
	public function add(){
		
		$supp=D('Descriptions')->where('tablename="server_buyer"')->field('fieldvalue as id,title')->select();
		
		
		if(IS_POST){

			    $_POST['r_time']=strtotime($_POST['r_time']);
			    $_POST['e_time']=strtotime($_POST['e_time']);
				$_POST['c_user']=is_login();
			 
				$web=D('Domain');

				$data=$web->create();				

				if($data){

					$id=$web->add();

					if($id){

						$this->success('新增成功',U('index'));

					}else{

						$this->error('新增失败');

					}

				}else{

					$this->error($web->getError());

				}

			}else{

				$bulider=new \Common\Builder\FormBuilder();

				$bulider->setMetaTitle('添加域名')//设置页面标题

				        ->setPostUrl(U('add'))//表单提交地址

						->addFormItem('name','text','域名','域名')

						->addFormItem('register','select2','注册商','注册商',$supp)

						->addFormItem('psd','text','管理密码','管理密码')

						 ->addFormItem('customer','user_list','客户 ID','客户 ID')
					 
                     
						->addFormItem('r_time','date','注册时间','注册时间')

						->addFormItem('e_time','date','过期时间','过期时间')
                        ->addFormItem('price','text','续费价格','续费价格')
						
                         ->addFormItem('remark','textarea','备注','备注')
					 												

				 
						 

						->display();

						 

			}
	}
	
	public function  edit($id){
			$supp=D('Descriptions')->where('tablename="server_buyer"')->field('fieldvalue as id,title')->select();
		
		$info=M('Domain')->find($id);

			if(IS_POST){

				$server=M('Domain');

				$_POST['r_time']=strtotime($_POST['r_time']);

				$_POST['e_time']=strtotime($_POST['e_time']);

			 

				$data=$server->create();

				if($data){

					$res=$server->save();

					if($res){

						$this->success('更新成功',U('index'));

					}else{

						$this->error('更新失败');

					}

				}else{

					$this->error($server->getError());

				}

			}else{

				$bulider=new \Common\Builder\FormBuilder();

				$bulider->setMetaTitle('编辑域名')

				        ->setPostUrl(U('edit'))

						->addFormItem('id','hidden','ID','ID')

				  ->addFormItem('name','text','域名','域名')

						->addFormItem('register','select2','注册商','注册商',$supp)

						->addFormItem('psd','text','管理密码','管理密码')

						 ->addFormItem('customer','user_list','客户 ID','客户 ID')
					 
                     
						->addFormItem('r_time','date','注册时间','注册时间')

						->addFormItem('e_time','date','过期时间','过期时间')
                        ->addFormItem('price','text','续费价格','续费价格')
						
                         ->addFormItem('remark','textarea','备注','备注')
											

						->setFormData($info)

						->display();

			}

	}
	
}
?>